//login.js
const app = getApp()

Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    email: '',
    showPassword: true,
    isAgree: false,
    isSubmit: true
  },
  onLoad: function () {
    var that = this;
    var wxUserId = app.globalData.wxUserId
    that.getPassWordStatus(wxUserId)
    /**
     * 获取当前设备的宽高
     */
    that.setData({
      winWidth: app.globalData.winWidth,
      winHeight: app.globalData.winHeight
    });
  },
  doLogin: function(e) { //下一步点击事件
    var that = this
    var formData = e.detail.value
    if(that.data.showPassword){
      if(formData.password){
        if(formData.password != formData.confimPassword){
          wx.showToast({
            title: '两次密码不一致',
            icon: "none"
          })
          return false;
        }
      }else {
        wx.showToast({
          title: '请输入密码',
          icon: "none"
        })
        return false;
      }
    }
    if(!that.data.isAgree){
      wx.showToast({
        title: '请勾选协议',
        icon: "none"
      })
      return false;
    }
    var data = {
      userId: app.globalData.wxUserId,
      email: formData.email,
      emailCode: formData.password
    }
    wx.showLoading({
      title: '提交中',
    })
    app.http.post('/wechat/addMailBox',data).then(res =>{
      console.log('------login提交结束----')
      wx.hideLoading()
      if(res.status == '200') {
        wx.navigateTo ({
          url: '../authorization/authorization'
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    })
  },
  emailJude: function(e) {
    var that = this
    if (e.detail.value.length > 0) {
      if(that.checkEmail(e.detail.value)) {// 邮箱格式检验
        that.setData({isSubmit: false})
      } else {
        wx.showToast({
          title: '请输入正确的邮箱格式',
          icon: "none"
        })
        that.setData({isSubmit: true})
      }
    } else {
      that.setData({isSubmit: true})
    }
  },
  checkboxChange: function(e) { //协议点击事件
    var that = this
    if(e.detail.value.length > 0 && e.detail.value[0] === 'agree') {
      that.setData({isAgree: true})
    } else {
      that.setData({isAgree: false})
    }
  },
  checkEmail: function(email) { //邮箱格式正则匹配
    var myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/
    if(myReg.test(email)){
　　  return true;
　　} else {
      return false;
    }
  },
  getPassWordStatus: function(wxUserId) {
    var that = this
    app.http.get('/wechat/getUserPassWord?wxUserId=' + wxUserId).then(res =>{
      if(res.status == '200') {
        var email = res.data.email
        var emails = true;
        if (email != null && email != '') {
          emails = false
        }
        that.setData({
          showPassword: res.data.password,
          email: email,
          isSubmit: emails,
        })
      }
      
    })
  }
})
